<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    /*
        串行执行
     */
    function sum(x) {
        return new Promise(resolve => setTimeout(resolve, 1000, x + 1))
    }

    // console.log(Array(10).fill(null).map(() => sum));

    // 串行执行: 在await的时候初始化promise
    async function add(x) {
        for (const fn of Array(5).fill(null).map(() => sum)) {
            console.log('我是串行')
            console.log('fn(x)',fn(x))
            x = await fn(x)
        }
        return x;
    }

    add(0).then(console.log);

    /*
        并行执行: 已经初始化好了promise
     */
    async function add2(...args) {
        const promises = Array.from({length: args.length}).map((_, i) => sum(args[i]))
        for (const p of promises) {
            console.log('我是并行')
            // 并行执行
            console.log('p',p)
            const result = await p;
            console.log(result)
        }
    }

    add2(0, 0, 0, 0, 0)

</script>
</body>
</html>